<?php
namespace app\wechat\controller\admin;
use app\common\controller\Admin;
use app\common\annotation\NodeAnnotation;
use app\common\traits\ControllerCurd;
use app\wechat\model\MenuModel;
use EasyWeChat\Factory;
use think\Request;

/**
 * @NodeAnnotation(
 *     title="自定义菜单",
 *     parent="wechat/admin.index",
 *     is_menu=0,
 *     is_view=0,
 *     auth=1,
 * )
 */
class Menus extends Admin
{
    use ControllerCurd;
    public $config;
    public $officailAccount;
    public $model;
    public function initialize()
    {
        parent::initialize();
        $this->model = new MenuModel();

        $config = config("official.");
        $option = [
            'app_id' => 'wx022d032893708397',
            'secret' => 'eba61ec2a2aeae9bcc6e1d21c6e1e1ec'
        ];
        //$option = get_option("wechat_official")['official'];
        $this->config= array_merge($config,$option);
        $this->officailAccount = Factory::officialAccount($this->config);

    }


    /**
     * @NodeAnnotation(
     *     title="菜单管理",
     *     parent="wechat/admin.menus",
     *     MenuRoot="wechat/admin.index"
     * )
     */
    public function index(Request $request)
    {
        if($request->isPost())
        {
            $menuList['menu']['button'] = $this->treeMenu($this->model->select());
            $this->success("munList",$request->url(),$menuList);
        }

        return $this->fetch();
    }

    /**
     * @NodeAnnotation(
     *     title="添加菜单",
     *     parent="wechat/admin.menus",
     *     MenuRoot="wechat/admin.index",
     *    is_menu=0,
     * )
     */
    public function addmenus(Request $request)
    {
        $addType = $request->param("addtype");
        if($addType=='add')
        {
            $input = $request->post();
            $input['key'] ="key_".getMsecTime();
            $this->add($request->url(),function($model,$data){
                $this->success('','',$data);
            },$input);
        }
        if ($addType=='puhsWechat')
        {
            $data = $this->model->select()->toArray();

            $buttons = $this->treeMenu($data);
            try{
                $res  = $this->officailAccount->menu->create($buttons);
                if($res['errcode']==0)
                {
                    throw new \Exception("操作成功",1);
                }else{
                    throw new \Exception($res['errmsg'],0);
                }
            }catch (\Exception $exception){
                if($exception->getCode()==1)
                {
                    $this->success($exception->getMessage());
                }
                $this->error($exception->getMessage());
            }
        }
    }



    /**
     * @NodeAnnotation(
     *     title="编辑菜单",
     *     parent="wechat/admin.menus",
     *     MenuRoot="wechat/admin.index",
     *    is_menu=0,
     * )
     */
    public function editmenus(Request $request)
    {
        $this->edit();

        $pramKey = $request->param("id");

        $data = $this->model->where("id",$pramKey)->find();

        $this->success("success",$request->url(),$data);

    }


    /**
     * @NodeAnnotation(
     *     title="删除菜单",
     *     parent="wechat/admin.menus",
     *     MenuRoot="wechat/admin.index",
     *    is_menu=0,
     * )
     */
    public function delmenus(Request $request)
    {
        $pramKey = $request->param("idList");
        $idList = [];

        if ($pramKey)
        {
            $idList = $this->model->where("id",$pramKey)
                ->whereOr("pid",$pramKey)
                ->column("id");
        }
        $this->del($request->url(),function($model,$data){
            $this->success("操作陈功");
        },$idList);
    }


    protected function treeMenu($data,$pid = 0)
    {
        $arr = [];
        foreach ($data as $v)
        {
            if($v['pid']==$pid)
            {
                $v['sub_button'] = $this->treeMenu($data,$v['id']);
                if(!$v['sub_button']){
                    unset($v['sub_button']);
                }
                $arr[] = $v;
            }
        }
        return  $arr;
    }

}